package dp.test5;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/22 10:44
 */
public class Solution1 {
    public String longestPalindrome(String s) {
        if (s == null || s.equals("")) return s;
        int rl = 0;
        int maxL = 1;
        int n = s.length();
        boolean[][] dp = new boolean[n][n];
        for (int len = 1; len <= n; len++) {
            for (int i = 0; i + len <= n; i++) {
                int j = i + len - 1;
                if (len == 1) {
                    dp[i][j] = true;
                } else if (s.charAt(i) == s.charAt(j)) {
                    dp[i][j] = j - i < 3 ? true : dp[i + 1][j - 1];
                }
                if (dp[i][j]) {
                    rl = i;
                    maxL = len;
                }
            }
        }
        return s.substring(rl, rl + maxL);
    }
}
